package com.esms.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.esms.dao.MonthlyAttendanceCustomVoMapper;
import com.esms.dao.MonthlyAttendanceMapper;
import com.esms.po.MonthlyAttendance;
import com.esms.service.impl.MonthlyAttendanceServiceImpl;
import com.esms.utils.Message;
import com.esms.vo.EmployeeMonthlyAttendancePages;
import com.esms.vo.MonthlyAttendanceCustomVo;
import com.esms.vo.MonthlyAttendancePages;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 考勤信息
 * @Author: lyy
 * @Date: 2021/10/9 8:08
 */
@Controller
@RequestMapping("/monthlyattendance")
public class MonthlyAttendanceController{
    @Autowired
    private MonthlyAttendanceServiceImpl monthlyAttendanceService;

    @Autowired
    private MonthlyAttendanceCustomVoMapper monthlyAttendanceCustomVoMapper = null;

    @Autowired
    private MonthlyAttendanceMapper monthlyAttendanceMapper = null;

    @RequestMapping("test.do")
    @ResponseBody
    public List<MonthlyAttendanceCustomVo> test(){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("e_account","4");
        map.put("d_id",4);
        map.put("attendance_time","2017-08");
        List<MonthlyAttendanceCustomVo> monthlyAttendanceCustomVos =
                monthlyAttendanceCustomVoMapper.selectMonthlyAttendanceCustomVoMapperByeAccountAnddIdAndTime(map);
        return monthlyAttendanceCustomVos;
    }


    /**
     * 查找
     * @param pageNum
     * @param limit
     * @param e_account
     * @param d_id
     * @param attendance_time
     * @return
     * @throws Exception
     */
    @RequestMapping("/findSelective.do")
    @ResponseBody
    public MonthlyAttendancePages findSelective(
            @RequestParam(value="page", defaultValue="1")int pageNum,
            @RequestParam(value="limit", defaultValue="5") int limit,
            @RequestParam(value="e_account", defaultValue="") String e_account,
            @RequestParam(value="d_id", defaultValue="0") int d_id,
            @RequestParam(value="attendance_time", defaultValue="") String attendance_time) throws Exception {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("e_account",e_account);
        map.put("d_id",d_id);
        map.put("attendance_time",attendance_time);
        //pageNum:起始页面  pageSize:每页的大小
        PageHelper.startPage(pageNum,limit);
        //查找条件，一定要紧跟在startPage后
        List<MonthlyAttendanceCustomVo> list =
                monthlyAttendanceCustomVoMapper.selectMonthlyAttendanceCustomVoMapperByeAccountAnddIdAndTime(map);
//        System.out.println(list.get(0).getMonthlyAttendance().getAttendanceTime()+"========================");
        PageInfo pageResult = new PageInfo(list);

        //设置前台需要的数据
        MonthlyAttendancePages monthlyAttendancePages = new MonthlyAttendancePages();
        monthlyAttendancePages.setCode(0);
        monthlyAttendancePages.setMsg("");
        monthlyAttendancePages.setCount((int) pageResult.getTotal());
        monthlyAttendancePages.setData(pageResult.getList());
        return monthlyAttendancePages;
    }

    /**
     *根据id查询考勤信息
     * @param id
     * @return
     */
    @RequestMapping("/selectVoByPrimaryKey.do")
    @ResponseBody
    public MonthlyAttendanceCustomVo selectVoByPrimaryKey(int id) {
        MonthlyAttendanceCustomVo monthlyAttendanceCustomVo = new MonthlyAttendanceCustomVo();
        monthlyAttendanceCustomVo = monthlyAttendanceCustomVoMapper.selectVoByPrimaryKey(id);
        return monthlyAttendanceCustomVo;
    }

    /**
     * 更新考勤信息 
     */
    @RequestMapping("/updateAttendance.do")
    @ResponseBody
    public Message updateByPrimaryKeySelective(MonthlyAttendance monthlyAttendance) throws Exception {
        monthlyAttendanceService.updateByPrimaryKeySelective(monthlyAttendance);
        return Message.success();
    }

    /**
     * 删除考勤信息
     * @param id
     * @throws Exception
     */
    @RequestMapping("/deleteByPrimaryKey.do")
    @ResponseBody
    public Message deleteByPrimaryKey(int id) throws Exception {
        MonthlyAttendance monthlyAttendance = new MonthlyAttendance();
        monthlyAttendance.setMaId(id);
        monthlyAttendanceService.deleteByPrimaryKey(id);
        return Message.success();
    }

    /**
     * 批量删除
     * @param ids
     */
    @RequestMapping("/deleteByQuery.do")
    @ResponseBody
    public Message deleteByQuery (@RequestParam(value = "arr")int[] ids) {
        //如果有id才执行
        if(ids.length > 0) {
            monthlyAttendanceService.deleteByQuery(ids);
        }
        return Message.success();

    }

    /**
     * 查询个人考勤信息
     * @param pageNum
     * @param limit
     * @param attendance_time
     * @param httpSession
     * @return
     * @throws Exception
     */
    @RequestMapping("/findEmployeeAttendance.do")
    @ResponseBody
    public EmployeeMonthlyAttendancePages findEmployeeAttendance(
            @RequestParam(value="page", defaultValue="1")int pageNum,
            @RequestParam(value="limit", defaultValue="5") int limit,
            @RequestParam(value="attendance_time", defaultValue="") String attendance_time,
            HttpSession httpSession) throws Exception {
        Integer id = (Integer) httpSession.getAttribute("employeeId");
        //pageNum:起始页面  pageSize:每页的大小
        PageHelper.startPage(pageNum,limit);
        //查找条件，一定要紧跟在startPage后
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("e_id",id);
        map.put("attendance_time",attendance_time);
        List<MonthlyAttendance> list = monthlyAttendanceMapper.selectMonthlyAttendanceMapperByeEidAndTime(map);
        PageInfo pageResult = new PageInfo(list);

        //设置前台需要的数据
        EmployeeMonthlyAttendancePages employeeMonthlyAttendancePages = new EmployeeMonthlyAttendancePages();
        employeeMonthlyAttendancePages.setCode(0);
        employeeMonthlyAttendancePages.setMsg("");
        employeeMonthlyAttendancePages.setCount((int) pageResult.getTotal());
        employeeMonthlyAttendancePages.setData(pageResult.getList());
        return employeeMonthlyAttendancePages;
    }
}
